package com.imu.purse.modules.sys.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.imu.common.exception.RRException;
import com.imu.constant.Constant;
import com.imu.purse.modules.sys.dao.SysConfigDao;
import com.imu.purse.modules.sys.entity.SysConfigEntity;
import com.imu.purse.modules.sys.service.SysConfigService;
import com.xiaoleilu.hutool.util.StrUtil;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service("sysConfigService")
public class SysConfigServiceImpl implements SysConfigService {
	@Autowired
	private SysConfigDao sysConfigDao;

	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	
	@Override
	@Transactional
	public void save(SysConfigEntity config) {
		sysConfigDao.save(config);
		redisTemplate.delete("sys:config");
	}

	@Override
	@Transactional
	public void update(SysConfigEntity config) {
		sysConfigDao.update(config);
		redisTemplate.delete("sys:config");
	}

	@Override
	@Transactional
	public void updateValueByKey(String key, String value) {
		sysConfigDao.updateValueByKey(key, value);
		redisTemplate.delete("sys:config");
	}

	@Override
	@Transactional
	public void deleteBatch(Long[] ids) {
		sysConfigDao.deleteBatch(ids);
		redisTemplate.delete("sys:config");
	}

	@Override
	public List<SysConfigEntity> queryList(Map<String, Object> map) {
		return sysConfigDao.queryList(map);
	}

	@Override
	public int queryTotal(Map<String, Object> map) {
		return sysConfigDao.queryTotal(map);
	}

	@Override
	public SysConfigEntity queryObject(Long id) {
		return sysConfigDao.queryObject(id);
	}

	@Override
	public String getValue(String key) {
		SysConfigEntity config = sysConfigDao.queryByKey(key);
		return config == null ? null : config.getValue();
	}

	@Override
	public SysConfigEntity findByKey(final String key)
    {
    	if(StrUtil.isBlank(key)) return null;
    	
    	SysConfigEntity entity = null;
		HashOperations<String, String, String> operations = redisTemplate.opsForHash();
    	String info = operations.get(Constant.REDIS_CFG_KEY, key);
    	if(StrUtil.isBlank(info))
    	{
    		entity = sysConfigDao.queryByKey(key);
    		if(null == entity || StrUtil.isBlank(entity.getKey())) return null;
    		
    		operations.put(Constant.REDIS_CFG_KEY, entity.getKey(), JSON.toJSONString(entity));
    		return entity;
    	}
    	
    	return JSON.parseObject(info, SysConfigEntity.class);
    }
	
	@Override
	public <T> T getConfigObject(String key, Class<T> clazz) {
		String value = getValue(key);
		if(StringUtils.isNotBlank(value)){
			return new Gson().fromJson(value, clazz);
		}

		try {
			return clazz.newInstance();
		} catch (Exception e) {
			throw new RRException("获取参数失败");
		}
	}
}
